package com.note.fastjson;

import com.alibaba.fastjson.JSON;

import java.io.*;

/**
 * @author RenGaoshuai
 * @date 2024/4/7 11:18
 * @description
 */
public class TestFastJson {

    public static void xuliehua(TestUser user) throws Exception{
        //生成一个文件对象,文件不存在将自动创建文件
        File f = new File("D:" + File.separator + "serTest.txt");

        //构造对象输出流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
        //序列化一个对象到文件变成二进制内容
        oos.writeObject(user);
        oos.close();

    }

    public static void fanxuliehua() throws Exception{
        //生成一个文件对象,文件不存在将自动创建文件
        File f = new File("D:" + File.separator + "serTest.txt");
        ObjectInputStream oi = new ObjectInputStream(new FileInputStream(f));
        Object o = oi.readObject();
        System.out.println(o);
        oi.close();
    }

    public static String beanToJson(TestUser user){
        return JSON.toJSONString(user);
    }

    public static void main(String[] args) throws Exception, ClassNotFoundException {
        TestUser testUser = new TestUser();
        testUser.setAge(12);
        testUser.setName("在格桑三");
        testUser.setUserUd(12365L);

        System.out.println(testUser);
//        System.out.println(testUser.toString());
        System.out.println(JSON.toJSONString(testUser));

        String userJson = "{\"age\":12,\"name\":\"在格桑三\",\"userUd\":12365}";
        TestUser testUser1 = JSON.parseObject(userJson, TestUser.class);
        System.out.println(testUser1);
        System.out.println(testUser == testUser1);
        System.out.println(testUser1.equals(testUser));

        fanxuliehua();



    }
}
